package com.auerdi.ui.account;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.auerdi.R;
import com.auerdi.ui.account.view.NumView;
import com.auerdi.ui.account.view.TestInutTextView;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.JoinREsult;
import com.example.egghttp.retrofit.params.JoinExpRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/2/14.
 */
public class ADOfficerRigisterFragment extends SwipeBackFragment {

    Unbinder unbinder;
    @BindView(R.id.title_left)
    RelativeLayout titleLeft;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.etName)
    TestInutTextView etName;
    @BindView(R.id.work)
    TestInutTextView work;
    @BindView(R.id.phone)
    NumView phone;
    @BindView(R.id.address)
    TestInutTextView address;
    @BindView(R.id.ability)
    TestInutTextView ability;
    @BindView(R.id.chckleft)
    ImageView chckleft;
    @BindView(R.id.chckright)
    ImageView chckright;
    @BindView(R.id.sex)
    RelativeLayout sex;

    private boolean isGirl = true;

    public static ADOfficerRigisterFragment newInstance() {

        Bundle args = new Bundle();

        ADOfficerRigisterFragment fragment = new ADOfficerRigisterFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ad_fragment_register_officer, container, false);


        unbinder = ButterKnife.bind(this, view);

        View tpview = attachToSwipeBack(view);


        return tpview;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                isGirl = !isGirl;

                chckleft.setImageResource(isGirl ? R.mipmap.check_ok : R.mipmap.check_no);
                chckright.setImageResource(!isGirl ? R.mipmap.check_ok : R.mipmap.check_no);
            }
        });
        etName.getEditText().setHint("姓名");
        work.getEditText().setHint("职业");
        phone.getEditText().setHint("手机");
        address.getEditText().setHint("地址");
        ability.getEditText().setHint("推广能力");
//        etName.getEditText().setText("18202164667");
//        etName.getEditText().setTextColor(Color.parseColor("#000000"));
//        etName.getEditText().setHintTextColor(Color.parseColor("#6F6F6F"));


        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                RetrofitClient.getPDAClient().create(AdService.class).experienceJoinexp(new JoinExpRequest(etName.getText(), phone.getText(), isGirl ? "女" : "男", address.getText(), "-1", ability.getText()))               //获取Observable对象
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(Schedulers.io())

                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BOb<JoinREsult>>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(BOb<JoinREsult> joinREsultBOb) {
                                L.d("hcia", "joinREsultBOb.getBody().getResult():" + joinREsultBOb.getBody().getResult());

                            }
                        });


            }
        });


    }

    @Override
    public void onDetach() {
        super.onDetach();
//        mOnLoginSuccessListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public interface OnLoginSuccessListener {
        void onLoginSuccess(String account);
    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        hideSoftInput();
    }
}
